package cn.xo68.boot.auth.server.web.api;

import cn.xo68.boot.auth.core.common.AuthConstants;
import java.util.List;

import cn.xo68.boot.auth.core.domain.Oauth2Principal;
import cn.xo68.boot.auth.server.domain.OauthClient;
import cn.xo68.boot.auth.server.domain.OauthResource;
import cn.xo68.core.common.TreeIdGenerater;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.StringTools;
import cn.xo68.core.util.UUIDUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xo68.boot.auth.server.service.OauthResourceService;



/** 
 * OauthResource Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年09月10日 12:42:18
 *
 */
@RequestMapping("/api/OauthResource")
@RestController
public class OauthResourceController{
    @Autowired
    private OauthResourceService oauthResourceService;

	@GetMapping("/find")
    public OauthResource find(@RequestParam(name="clientId") String clientId,@RequestParam(name="id") String id) {
        return oauthResourceService.get(clientId, id);
    }
    
    @GetMapping("/list")
    public PageInfo<OauthResource> list(@RequestParam(name="clientId") String clientId,@RequestParam(name="pageIndex",defaultValue="1") int pageIndex, @RequestParam(name="pageSize",defaultValue="10") int pageSize){
        return oauthResourceService.list(clientId, pageIndex, pageSize);
    }
    @GetMapping("/all")
    public List<OauthResource> listAll(@RequestParam(name="clientId") String clientId){
        return oauthResourceService.list(clientId);
    }
    @GetMapping("/child")
    public List<OauthResource> listchild(@RequestParam(name="clientId") String clientId, @RequestParam(name="parentId") String parentId){
        return oauthResourceService.listchild(clientId,parentId);
    }
	
	@PostMapping("/add")
    public String add(@RequestBody OauthResource entity, Oauth2Principal oauth2Principal ) throws Exception {
        if(StringTools.isEmpty(entity.getResourceId())){
            OauthResource maxBrother=oauthResourceService.getMaxChild(entity.getClientId(),entity.getParentId());
            if(maxBrother==null){
                String id=TreeIdGenerater.parse(entity.getParentId()).firstChild().asString();
                entity.setResourceId(id);
            }else {
                if(entity.getParentId()!=null && !entity.getParentId().equals(TreeIdGenerater.parse(maxBrother.getResourceId()).getParent().asString())){
                    throw  new Exception("资源信息结构不规范");
                }
                String id=TreeIdGenerater.parse(maxBrother.getResourceId()).next().asString();
                entity.setResourceId(id);
            }

        }
        entity.setOperatorId(oauth2Principal.getUserId());
        entity.setOperatorName(oauth2Principal.getUserName());
        entity.setCreateTime(DateTime.Now().getDate());
        entity.setUpdateTime(DateTime.Now().getDate());
        oauthResourceService.insert(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
    }
	
	@PostMapping("/delete")
	public String delete(@RequestParam(name="clientId") String clientId,@RequestParam(name="id") String id) {
	    oauthResourceService.delete(clientId, id);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
	
	@PostMapping("/update")
	public String update(@RequestBody OauthResource entity, Oauth2Principal oauth2Principal) {
        entity.setOperatorId(oauth2Principal.getUserId());
        entity.setOperatorName(oauth2Principal.getUserName());
        entity.setUpdateTime(DateTime.Now().getDate());
	    oauthResourceService.update(entity);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
}